<?xml version="1.0" ?>

<xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
						xmlns:lzc="http://schemas.icitadel.com/Lazarus/Compiled.xsd"
                  version="1.0">

  <xsl:output method="text"/>

  <xsl:include href="../../common.xsl" />
  <xsl:include href="../../convert.xsl" />

  <xsl:template match="lzc:project">
    <xsl:apply-templates select="lzc:objects/lzc:object[ ( @compiling ) ]" />
  </xsl:template>

  <xsl:template match="lzc:object[ @compiling ]">

    <xsl:variable name="v_PKField" select="lzc:fields/lzc:field[ .//lzc:index/@type = 'Primary' ]" />
    <xsl:variable name="v_collections" select="lzc:collections/lzc:collection" />

    <xsl:variable name="v_ObjectName" select="@name" />
    <xsl:variable name="v_ObjectLogicalName" select="@logical-name" />

    <xsl:variable name="v_ClassName">
      <xsl:value-of select="$v_ObjectName" />
    </xsl:variable>

    <xsl:variable name="v_DetailName">
      <xsl:value-of select="$v_ClassName" />
    </xsl:variable>
    
    <xsl:variable name="v_CollectionName">
      <xsl:value-of select="@name" />
      <xsl:text>Collection</xsl:text>
    </xsl:variable>
    
    <xsl:variable name="v_NamespacePrefix">
      <xsl:if test="/lzc:project/@namespaceprefix">
        <xsl:value-of select="/lzc:project/@namespaceprefix" />
        <xsl:text>:</xsl:text>
      </xsl:if>
    </xsl:variable>
    
    <xsl:variable name="v_RuntimeNamespace">
      <xsl:if test="ancestor::lzc:project/@defaultnamespace">
        <xsl:value-of select="ancestor::lzc:project/@defaultnamespace"/>
      </xsl:if>
    </xsl:variable>
    
    <xsl:variable name="v_BusinessConceptNamespace">
      <xsl:if test="/lzc:project/@businessconcept">
        <xsl:text>.</xsl:text>
        <xsl:value-of select="/lzc:project/@businessconcept" />
      </xsl:if>
    </xsl:variable>
    
    <xsl:variable name="v_DataManagerClassName">
      <xsl:text>Derived.</xsl:text>
      <xsl:value-of select="/lzc:project/@name" />
      <xsl:text>DataManager</xsl:text>
    </xsl:variable>

    <xsl:variable name="v_AncestorClass">
      <xsl:text>ModelElementBase&lt;</xsl:text>
      <xsl:call-template name="getNETType">
        <xsl:with-param name="pFieldType" select="$v_PKField/@type" />
      </xsl:call-template>
      <xsl:text>, </xsl:text>
      <xsl:value-of select="$v_DataManagerClassName" />
      <xsl:text>&gt;</xsl:text>
    </xsl:variable>

    <xsl:text>using System;
using System.Data;
using System.Diagnostics;
using System.Xml;

using OR.Reuse.Persistence;

using DataAccess = </xsl:text>
    <xsl:value-of select="$v_RuntimeNamespace" />
    <xsl:text>.DataAccess</xsl:text>
    <xsl:value-of select="$v_BusinessConceptNamespace" />
    <xsl:text>;
using Derived = </xsl:text>
    <xsl:value-of select="$v_RuntimeNamespace" />
    <xsl:text>.Model</xsl:text>
    <xsl:value-of select="$v_BusinessConceptNamespace" />
    <xsl:text>;

</xsl:text>

    <xsl:text>namespace </xsl:text>
    <xsl:value-of select="$v_RuntimeNamespace" />
    <xsl:text>.Model</xsl:text>
    <xsl:value-of select="$v_BusinessConceptNamespace" />
    <xsl:text>.Generated
{
	/// &lt;summary&gt;
	/// </xsl:text><xsl:value-of select="lzc:description" /><xsl:text>
	/// &lt;/summary&gt;
   public class </xsl:text><xsl:value-of select="$v_DetailName" /><xsl:text> : </xsl:text>
    <xsl:choose>
      <xsl:when test=".//lzc:implement">
        <xsl:text>Derived.</xsl:text>
        <xsl:value-of select=".//lzc:implement[ position() = 1 ]/@ref" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$v_AncestorClass" />
      </xsl:otherwise>
    </xsl:choose>
    {
    
        #region Private Storage
    <xsl:for-each select="lzc:fields/lzc:field[ not( .//lzc:index[ ( @type = $INDEXTYPE_FOREIGN ) or ( @type = $INDEXTYPE_PRIMARY ) ] ) ]">
      <xsl:text>
		    private </xsl:text>
      <xsl:call-template name="getNETParamType">
        <xsl:with-param name="pFieldType" select="@type" />
        <xsl:with-param name="pRequired" select="@required" />
      </xsl:call-template>
      <xsl:text> m</xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text>;</xsl:text>
    </xsl:for-each>

    <xsl:if test="lzc:fields/lzc:field[ .//lzc:index[ @type = $INDEXTYPE_FOREIGN ] ]">
      <xsl:text>
	</xsl:text>
    </xsl:if>

    <xsl:for-each select="lzc:fields/lzc:field[ .//lzc:index[ @type = $INDEXTYPE_FOREIGN ] ]">
      <xsl:text>
		    private Derived.</xsl:text>
      <xsl:value-of select=".//lzc:index[ @type = $INDEXTYPE_FOREIGN ]/@object" />
      <xsl:text> m</xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text>;</xsl:text>
    </xsl:for-each>

    <xsl:if test="lzc:collections/lzc:collection[ not( @name = $v_ObjectName ) ]">
      <xsl:text>
	</xsl:text>
    </xsl:if>

    <xsl:for-each select="lzc:collections/lzc:collection[ not( @name = $v_ObjectName ) ]">
      <xsl:text>
		    private Derived.</xsl:text><xsl:value-of select="@object" />Collection m<xsl:value-of select="@name" /><xsl:text>;</xsl:text>
    </xsl:for-each>

    <xsl:text>
        #endregion

        public </xsl:text>
    <xsl:value-of select="$v_ClassName" />(<xsl:value-of select="$v_DataManagerClassName" />
    <xsl:text> manager) : base(manager)
        { }

</xsl:text>

    <xsl:text>
		    public </xsl:text><xsl:value-of select="$v_DetailName" /><xsl:text>()
		    {</xsl:text>
    <xsl:for-each select="lzc:collections/lzc:collection[ not( @name = $v_ObjectName ) ]">
      <xsl:text>
			      m</xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text> = new Derived.</xsl:text>
      <xsl:value-of select="@object" />
      <xsl:text>Collection(this.DataManager);</xsl:text>
    </xsl:for-each>
    <xsl:text>
	</xsl:text>
    <xsl:for-each select="lzc:fields/lzc:field[ .//lzc:index[ @type = $INDEXTYPE_FOREIGN ] ]">
      <xsl:text>
			      m</xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text> = new Derived.</xsl:text>
      <xsl:value-of select=".//lzc:index[ @type = $INDEXTYPE_FOREIGN ]/@object" />
      <xsl:text>(this.DataManager);</xsl:text>
    </xsl:for-each>
    <xsl:text>
		    }
	</xsl:text>
    <xsl:for-each select="lzc:fields/lzc:field[ not( .//lzc:index[ @type = $INDEXTYPE_PRIMARY ] ) ]">
      <xsl:text>
		    /// </xsl:text>&lt;summary&gt;<xsl:text>
		    /// </xsl:text>
      <xsl:value-of select="lzc:description" />
      <xsl:text>
		    /// </xsl:text>&lt;/summary&gt;<xsl:text>
		    public </xsl:text>
      <xsl:choose>
        <xsl:when test="not( .//lzc:index[ @type = $INDEXTYPE_FOREIGN ] )">
          <xsl:call-template name="getNETParamType">
            <xsl:with-param name="pFieldType" select="@type" />
            <xsl:with-param name="pRequired" select="@required" />
          </xsl:call-template>
          <xsl:value-of select="$space" />
          <xsl:value-of select="@logical-name" />
          <xsl:text>
		    {
			      get { return ( m</xsl:text><xsl:value-of select="@name" /> ); }
            set { m<xsl:value-of select="@name" /><xsl:text> = value; }
		    }
</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="v_szFieldType">
            <xsl:text>Derived.</xsl:text>
            <xsl:value-of select=".//lzc:index[ @type = $INDEXTYPE_FOREIGN ]/@object" />
          </xsl:variable>
          <xsl:variable name="v_szFieldName">
            <xsl:value-of select="@name" />
          </xsl:variable>
          <xsl:value-of select="$v_szFieldType" />
          <xsl:value-of select="$space" />
          <xsl:value-of select="$v_szFieldName" />
          <xsl:text>
		    {
			      get { return ( m</xsl:text><xsl:value-of select="$v_szFieldName" /> ); }
            set { m<xsl:value-of select="$v_szFieldName" /><xsl:text> = value; }
		    }
</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
    <xsl:for-each select="lzc:collections/lzc:collection[ not( @name = $v_ObjectName ) ]">
      <xsl:text>
		    public Derived.</xsl:text><xsl:value-of select="@object" />Collection <xsl:value-of select="@name" /><xsl:text>
		    {
			      get { return ( m</xsl:text><xsl:value-of select="@name" /> ); }
            set { m<xsl:value-of select="@name" /><xsl:text> = value; }
		    }
</xsl:text>
    </xsl:for-each>
    <xsl:for-each select="//lzc:table[ @name = $v_ObjectName ]">
      <xsl:if test="position() = 1">
        public override void Refresh()
        {
        <xsl:choose>
          <xsl:when test="not(.//lzc:method[ @methodtype = 'Get' ])">
            <xsl:text>throw new NotSupportedException();</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>DataAccess.</xsl:text><xsl:value-of select="$v_ObjectName" /> access<xsl:value-of select="$v_ObjectName" /> = new DataAccess.<xsl:value-of select="$v_ObjectName" /><xsl:text>();
           </xsl:text>

            <xsl:for-each select=".//lzc:method[ @methodtype = 'Get' ]">
              <xsl:if test="position() = 1">
                DataRow currentRow = access<xsl:value-of select="$v_ObjectName" />.GetRow(this.Id);

                this.Load(currentRow);
              </xsl:if>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
        }

        public override void Load(DataRow sourceRow)
        {
        <xsl:choose>
          <xsl:when test="not(.//lzc:method[ @methodtype = 'Get' ])">
            <xsl:text>throw new NotSupportedException();
            </xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select=".//lzc:method[ @methodtype = 'Get' ]">
              <xsl:if test="position() = 1">
                <xsl:for-each select=".//lzc:method-result">
                  <xsl:text>
            </xsl:text>
                  <xsl:text>this.</xsl:text>
                  <xsl:value-of select="@field" />
                  <xsl:if test="@isfk = 'True'">
                    <xsl:text>.Id</xsl:text>
                  </xsl:if>
                  <xsl:text> = </xsl:text>
                  <xsl:call-template name="getDataUtilityFunction">
                    <xsl:with-param name="pFieldType" select="@type" />
                    <xsl:with-param name="pSourceObject">sourceRow</xsl:with-param>
                    <xsl:with-param name="pTargetObject" select="@dbname" />
                    <xsl:with-param name="pDefault" select="@default" />
                    <xsl:with-param name="pRequired" select="'True'" />
                  </xsl:call-template>
                  <xsl:text>;</xsl:text>
                </xsl:for-each>
              </xsl:if>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
        }

        protected override void SaveNew()
        {
        <xsl:choose>
          <xsl:when test="not(.//lzc:method[ @methodtype = 'Create' ])">
            <xsl:text>throw new NotSupportedException();</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>DataAccess.</xsl:text><xsl:value-of select="$v_ObjectName" /> access<xsl:value-of select="$v_ObjectName" /> = new DataAccess.<xsl:value-of select="$v_ObjectName" /><xsl:text>();
           
           </xsl:text>

            <xsl:for-each select=".//lzc:method[ @methodtype = 'Create' ]">
              <xsl:if test="position() = 1">
                <xsl:for-each select=".//lzc:method-param[ @direction = 'out' ]">
                  <xsl:call-template name="getNETParamType">
                    <xsl:with-param name="pFieldType" select="@type" />
                    <xsl:with-param name="pRequired" select="@required" />
                  </xsl:call-template>
                  <xsl:text> new</xsl:text>
                  <xsl:value-of select="@fieldname" />
                  <xsl:text>;</xsl:text>
                  <xsl:text>
           </xsl:text>
                </xsl:for-each>

                <xsl:text>access</xsl:text>
                <xsl:value-of select="$v_ObjectName" />
                <xsl:text>.Create(</xsl:text>
                <xsl:for-each select=".//lzc:method-param">
                  <xsl:if test="position() &gt; 1">
                    <xsl:text>, </xsl:text>
                  </xsl:if>
                  <xsl:choose>
                    <xsl:when test="@direction = 'out'">
                      <xsl:text>out new</xsl:text>
                      <xsl:value-of select="@fieldname" />
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:text>this.</xsl:text>
                      <xsl:value-of select="@fieldname" />
                      <xsl:if test="@isfk = 'True'">
                        <xsl:text>.Id</xsl:text>
                      </xsl:if>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:for-each>
                <xsl:text>);
           </xsl:text>
              </xsl:if>
            </xsl:for-each>

            <xsl:for-each select=".//lzc:method-param[ @direction = 'out' ]">
              <xsl:text>
          this.</xsl:text>
              <xsl:value-of select="@fieldname" />
              <xsl:text> = new</xsl:text>
              <xsl:value-of select="@fieldname" />
              <xsl:text>;</xsl:text>
              <xsl:text>
           </xsl:text>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
        }

        protected override void SaveExisting()
        {
        <xsl:choose>
          <xsl:when test="not(.//lzc:method[ @methodtype = 'Update' ])">
            <xsl:text>throw new NotSupportedException();</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>DataAccess.</xsl:text><xsl:value-of select="$v_ObjectName" /> access<xsl:value-of select="$v_ObjectName" /> = new DataAccess.<xsl:value-of select="$v_ObjectName" /><xsl:text>();
           </xsl:text>

            <xsl:for-each select=".//lzc:method[ @methodtype = 'Update' ]">
              <xsl:if test="position() = 1">
                <xsl:for-each select=".//lzc:method-param[ @direction = 'out' ]">
                  <xsl:call-template name="getNETParamType">
                    <xsl:with-param name="pFieldType" select="@type" />
                    <xsl:with-param name="pRequired" select="@required" />
                  </xsl:call-template>
                  <xsl:text> new</xsl:text>
                  <xsl:value-of select="@fieldname" />
                  <xsl:text>;</xsl:text>
                  <xsl:text>
           </xsl:text>
                </xsl:for-each>

                access<xsl:value-of select="$v_ObjectName" />
                <xsl:text>.Update(</xsl:text>
                <xsl:for-each select=".//lzc:method-param">
                  <xsl:if test="position() &gt; 1">
                    <xsl:text>, </xsl:text>
                  </xsl:if>
                  <xsl:choose>
                    <xsl:when test="@direction = 'out'">
                      <xsl:text>out new</xsl:text>
                      <xsl:value-of select="@fieldname" />
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:text>this.</xsl:text>
                      <xsl:value-of select="@fieldname" />
                      <xsl:if test="@isfk = 'True'">
                        <xsl:text>.Id</xsl:text>
                      </xsl:if>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:for-each>
                <xsl:text>);
           </xsl:text>
              </xsl:if>

              <xsl:for-each select=".//lzc:method-param[ @direction = 'out' ]">
                <xsl:text>
          this.</xsl:text>
                <xsl:value-of select="@fieldname" />
                <xsl:text> = new</xsl:text>
                <xsl:value-of select="@fieldname" />
                <xsl:text>;</xsl:text>
                <xsl:text>
           </xsl:text>
              </xsl:for-each>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
        }
      </xsl:if>
    </xsl:for-each>
    }
    }
  </xsl:template>

</xsl:stylesheet>